# 封装celery任务函数
import logging

from celery_tasks.main import celery_app
from celery_tasks.yuntongxun import constants
from celery_tasks.yuntongxun.sms import CCP

# 获取在配置文件中定义的logger，用来记录日志
logger = logging.getLogger('django')

@celery_app.task(name='send_sms_code')
def send_sms_code(mobile,sms_code,expires):
    # 使用云通讯给mobile发送短信
    # expires = constants.SMS_CODE_REDIS_EXPIRES // 60
    try:
        res = CCP().send_template_sms(mobile, [sms_code,expires], constants.SMS_CODE_TEMP_ID)
    except Exception as e:
        logger.error('短信验证码发送异常：[mobile: %s sms_code: %s]' %(mobile,sms_code))
        # 因为这边是异步发送短信，所以不需要这种相应，只需要返回日志即可
        # return Response({"message": "发送短信异常"}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
    else:
        if res != 0:
            logger.error('短信验证码发送失败：[mobile: %s sms_code: %s]' % (mobile, sms_code))
        else:
            logger.info('短信验证码发送OK：[mobile: %s sms_code: %s]' % (mobile, sms_code))
            # logger.error('Error Message')
        # 因为这边是异步发送短信，所以不需要这种相应，只需要返回日志即可
        # return Response({"message": "发送短信失败"}, status=status.HTTP_503_SERVICE_UNAVAILABLE)


